package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.PtyStuGroupMapper;
import cn.wzut.edut.dao.mapper.SysDeptMapper;
import cn.wzut.edut.dao.mapper.SysDeptUserRelMapper;
import cn.wzut.edut.dao.mapper.SysUserMapper;
import cn.wzut.edut.dao.pojo.table.*;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 组织管理
 */
@Repository
public class Dept {
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysDeptUserRelMapper sysDeptUserRelMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    //----------------------------分页查询所有的xxx---------------------------------

    /**
     * 分页查询所有虚拟班级
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysDept> selectAllStuGroupPage(long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_type","虚拟班级");
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页查询所有行政班级
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysDept> selectAllClassPage(long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_type","行政班级");
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页查询所有学院
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysDept> selectAllControllerPage(long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_type","学院");
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页查询所有教师组
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysDept> selectAllTecGroupPage(long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_type","教师组");
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页查询所有学校
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysDept> selectAllSchoolPage(long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_type","学校");
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页查询所有学校
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysDept> selectAllPage(long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),null).getRecords();
    }
    //----------------------------查询所有的xxx---------------------------------
    /**
     * 查询所有学院
     */
    public List<SysDept> selectAllController(){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_type","学院");
        return sysDeptMapper.selectList(queryWrapper);
    }
    /**
     * 查询该学院下的所有行政班级
     * 传入学院名称
     */
    public List<SysDept> selectClassInController(String name){
        /**
         * 查询学院的id
         */
        QueryWrapper<SysDept> q1 = new QueryWrapper<>();
        q1.select("id").in("dept_name",name).in("dept_type","学院");
        String id = sysDeptMapper.selectOne(q1).getId();
        /**
         * 查询父目录id为该学院id的行政班级
         */
        QueryWrapper<SysDept> q2 = new QueryWrapper<>();
        q2.in("parent_dept_id",id).in("dept_type","行政班级");
        return sysDeptMapper.selectList(q2);
    }
//    /**
//     * 按组织查询用户组织关系表
//     * 传入学院、班级
//     */
//    public List<String> selectUserIdInDept(String controller,String classname){
//        /**
//         * 当有班级时查询班级
//         */
//        if(classname != null) {
//            /**
//             * 查询学院的id
//             */
//            QueryWrapper<SysDept> q1 = new QueryWrapper<>();
//            q1.select("id").in("dept_name", controller).in("dept_type", "学院");
//            String id = sysDeptMapper.selectOne(q1).getId();
//            QueryWrapper<SysDept> q2 = new QueryWrapper<>();
//            /**
//             * 查询父目录id为该学院id的行政班级
//             */
//            q2.in("parent_dept_id", id).in("dept_type", "行政班级").in("dept_name", classname);
//            String classid = sysDeptMapper.selectOne(q2).getId();
//            /**
//             * 查询该行政班级id下的用户
//             */
//            QueryWrapper<SysDeptUserRel> q3 = new QueryWrapper<>();
//            q3.in("dept_id", classid);
//            List<SysDeptUserRel> list1 = sysDeptUserRelMapper.selectList(q3);
//            List<String> userids = new LinkedList<>();
//            for(int i = 0;i<userids.size();i++){
//                userids.add(list1.get(i).getUserId());
//            }
//            return userids;
//        }
//        /**
//         * 当没有班级时，查询学院下的用户和学院下行政班级的所有用户
//         */
//        else{
//            /**
//             * 查询学院的id
//             */
//            QueryWrapper<SysDept> q1 = new QueryWrapper<>();
//            q1.select("id").in("dept_name", controller).in("dept_type", "学院");
//            List<String> listid = new LinkedList<>();
//            /**
//             * 查询父目录id为该学院id的所有行政班级
//             */
//            listid.add(sysDeptMapper.selectOne(q1).getId());
//            List<SysDept> list = selectClassInController(controller);
//            /**
//             * 将组织id列为一个列表
//             */
//            for(int i = 0;i<list.size();i++){
//                listid.add(list.get(i).getId());
//            }
//            /**
//             * 按组织id列表去重查询
//             */
//            QueryWrapper<SysDeptUserRel> q3 = new QueryWrapper<>();
//            q3.select("DISTINCT id,dept_id,user_id").in("dept_id", listid);
//            List<SysDeptUserRel> list1 = sysDeptUserRelMapper.selectList(q3);
//            List<String> userids = new LinkedList<>();
//            for(int i = 0;i<userids.size();i++){
//                userids.add(list1.get(i).getUserId());
//            }
//            return userids;
//        }
//    }

    /**
     * 按组织查询用户组织关系表
     * 分页
     * 传入学院、班级,需要的页码current和每页显示的数量size
     */
    public List<SysUser> selectUserInDeptPage(String deptid, String name,long current,long size) {
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",deptid);
        List<SysDeptUserRel> list1 = sysDeptUserRelMapper.selectList(queryWrapper);
        List<SysUser> list = new LinkedList<>();
        for(int i = 0;i<list1.size();i++){
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",list1.get(i).getUserId());
            list.add(sysUserMapper.selectOne(queryWrapper1));
        }
        List<SysUser> list2 = new LinkedList<>();
        for(long i = current*size-size;i<current*size;i++){list2.add(list.get((int) i));}
        return list2;
    }

    /**
     * 更改用户组织
     * 传入用户id，原来的组织id，更改后的组织id
     */
    public boolean updateUserDept(String userid,String old,String news){
        QueryWrapper<SysDeptUserRel> q1 = new QueryWrapper<>();
        q1.in("user_id",userid).in("dept_id",old);
        SysDeptUserRel sysDeptUserRel = sysDeptUserRelMapper.selectOne(q1);
        sysDeptUserRel.setDeptId(news);
        QueryWrapper<SysDeptUserRel> q2 = new QueryWrapper<>();
        q2.in("id",sysDeptUserRel.getId());
        sysDeptUserRelMapper.update(sysDeptUserRel,q2);
        return true;
    }

    /**
     * 分页查询所有组织
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysDept> selectAllDept(long current,long size){
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),null).getRecords();
    }
    /**
     * 查询该学院下的所有行政班级
     * 传入学院名称
     */
    public List<SysDept> selectDeptInName(String name){
        /**
         * 查询学院的id
         */
        QueryWrapper<SysDept> q1 = new QueryWrapper<>();
        q1.in("dept_name",name);
        return sysDeptMapper.selectList(q1);
    }

    /**
     * 将用户添加进组织
     */
    public boolean delUserDept(String userid,String deptid){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userid).in("dept_id",deptid);
        sysDeptUserRelMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 凭借组织id和用户名查询组织内用户
     */
    public List<SysUser> selectUserInDeptInUserNamePage(String realname, String deptid, long current, long size){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",deptid);
        List<SysDeptUserRel> list = sysDeptUserRelMapper.selectList(queryWrapper);
        List<SysUser> list1 = new LinkedList();
        for (int i = 0;i<list.size();i++){
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("real_name",realname).in("id",list.get(i).getUserId());
            list1.add(sysUserMapper.selectOne(queryWrapper1));}
        List<SysUser> list2 = new LinkedList<>();
        for(long i = current*size-size;i<current*size;i++){list2.add(list1.get((int) i));}
        return list2;
    }
    /**278
     * 查询该id名下的所有组织
     */
    public List<SysDept> selectDeptInId(String createrId){
        /**
         * 查询学院的id
         */
        QueryWrapper<SysDept> q1 = new QueryWrapper<>();
        q1.in("creater_id",createrId);
        return sysDeptMapper.selectList(q1);
    }
    /**
     * 删除组织
     */
    public boolean delDept(String id){
        QueryWrapper<SysDeptUserRel> q1 = new QueryWrapper<>();
        q1.in("dept_id",id);
        sysDeptUserRelMapper.delete(q1);
        QueryWrapper<SysDept> q2 = new QueryWrapper<>();
        q2.in("id",id);
        sysDeptMapper.delete(q2);
        return true;
    }

    /**
     * 按组织查询用户组织关系表人数
     * 分页
     * 传入学院、班级,需要的页码current和每页显示的数量size
     */
    public long countUserInDeptPage(String deptid, String name,long current,long size) {
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",deptid);
        return sysDeptUserRelMapper.selectCount(queryWrapper);

    }
    /**328
     * 总数
     * 凭借组织id和用户名查询组织内用户
     */
    public long countUserInDeptInUserNamePage(String realname, String deptid){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",deptid);
        List<SysDeptUserRel> list = sysDeptUserRelMapper.selectList(queryWrapper);
        List<SysUser> list1 = new LinkedList();
        for (int i = 0;i<list.size();i++){
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("real_name",realname).in("id",list.get(i).getUserId());
            list1.add(sysUserMapper.selectOne(queryWrapper1));}
        return list1.size();

    }

    /**
     * 获取一个组织里的所有人
     */
    public List<SysUser> selectUserInDept(String deptid, long current, long size){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",deptid);
        List<SysDeptUserRel> list = sysDeptUserRelMapper.selectList(queryWrapper);
        List<SysUser> list1 = new LinkedList();
        for (int i = 0;i<list.size();i++){
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",list.get(i).getUserId());
            list1.add(sysUserMapper.selectOne(queryWrapper1));}
        List<SysUser> list2 = new LinkedList<>();
        for(long i = current*size-size;i<current*size;i++){list2.add(list1.get((int) i));}
        return list2;
    }

    /**
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     * 已整理
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础查询
     * ===================================================================================================
     */

    /**
     * 查询组织信息
     */
    public List<SysDept> selectSysDept(String id,String dept_name,String parent_dept_id,String dept_type,String creater_id,String del_flag){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(dept_name),"dept_name",dept_name);
        queryWrapper.in(!StringUtils.isEmpty(parent_dept_id),"parent_dept_id",parent_dept_id);
        queryWrapper.in(!StringUtils.isEmpty(dept_type),"dept_type",dept_type);
        queryWrapper.in(!StringUtils.isEmpty(creater_id),"creater_id",creater_id);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        return sysDeptMapper.selectList(queryWrapper);
    }
    /**
     * 查询组织信息数量
     */
    public long countSysDept(String id,String dept_name,String parent_dept_id,String dept_type,String creater_id,String del_flag){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(dept_name),"dept_name",dept_name);
        queryWrapper.in(!StringUtils.isEmpty(parent_dept_id),"parent_dept_id",parent_dept_id);
        queryWrapper.in(!StringUtils.isEmpty(dept_type),"dept_type",dept_type);
        queryWrapper.in(!StringUtils.isEmpty(creater_id),"creater_id",creater_id);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        return sysDeptMapper.selectCount(queryWrapper);
    }
    /**
     * 查询组织信息
     * 分页
     */
    public List<SysDept> selectSysDeptPage(String id,String dept_name,String parent_dept_id,String dept_type,String creater_id,String del_flag,long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(dept_name),"dept_name",dept_name);
        queryWrapper.in(!StringUtils.isEmpty(parent_dept_id),"parent_dept_id",parent_dept_id);
        queryWrapper.in(!StringUtils.isEmpty(dept_type),"dept_type",dept_type);
        queryWrapper.in(!StringUtils.isEmpty(creater_id),"creater_id",creater_id);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询组织信息
     * 列表查询
     */
    public List<SysDept> selectSysDeptList(List<String> id,List<String> dept_name,List<String> parent_dept_id,List<String> dept_type,List<String> creater_id,List<String> del_flag){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_name!=null,"dept_name",dept_name);
        queryWrapper.in(parent_dept_id!=null,"parent_dept_id",parent_dept_id);
        queryWrapper.in(dept_type!=null,"dept_type",dept_type);
        queryWrapper.in(creater_id!=null,"creater_id",creater_id);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        return sysDeptMapper.selectList(queryWrapper);
    }
    /**
     * 查询组织信息数量
     * 列表查询
     */
    public long countSysDeptList(List<String> id,List<String> dept_name,List<String> parent_dept_id,List<String> dept_type,List<String> creater_id,List<String> del_flag){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_name!=null,"dept_name",dept_name);
        queryWrapper.in(parent_dept_id!=null,"parent_dept_id",parent_dept_id);
        queryWrapper.in(dept_type!=null,"dept_type",dept_type);
        queryWrapper.in(creater_id!=null,"creater_id",creater_id);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        return sysDeptMapper.selectCount(queryWrapper);
    }
    /**
     * 查询组织信息
     * 列表查询
     * 分页
     */
    public List<SysDept> selectSysDeptListPage(List<String> id,List<String> dept_name,List<String> parent_dept_id,List<String> dept_type,List<String> creater_id,List<String> del_flag,long current,long size){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_name!=null,"dept_name",dept_name);
        queryWrapper.in(parent_dept_id!=null,"parent_dept_id",parent_dept_id);
        queryWrapper.in(dept_type!=null,"dept_type",dept_type);
        queryWrapper.in(creater_id!=null,"creater_id",creater_id);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        return sysDeptMapper.selectPage(new Page<SysDept>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询用户组织信息
     */
    public List<SysDeptUserRel> selectSysDeptUser(String id,String dept_id,String user_id){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",id);
        queryWrapper.in("dept_id",dept_id);
        queryWrapper.in("user_id",user_id);
        return sysDeptUserRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询用户组织信息数量
     */
    public long countSysDeptUser(String id,String dept_id,String user_id){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(dept_id),"dept_id",dept_id);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return sysDeptUserRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询用户组织信息
     * 分页
     */
    public List<SysDeptUserRel> selectSysDeptUserPage(String id,String dept_id,String user_id,long current,long size){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(dept_id),"dept_id",dept_id);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return sysDeptUserRelMapper.selectPage(new Page<SysDeptUserRel>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询用户组织信息
     * 列表查询
     */
    public List<SysDeptUserRel> selectSysDeptUserList(List<String> id,List<String> dept_id,List<String> user_id){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_id!=null,"dept_id",dept_id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return sysDeptUserRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询用户组织信息数量
     * 列表查询
     */
    public long countSysDeptUserList(List<String> id,List<String> dept_id,List<String> user_id){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_id!=null,"dept_id",dept_id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return sysDeptUserRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询用户组织信息
     * 列表查询
     * 分页
     */
    public List<SysDeptUserRel> selectSysDeptUserListPage(List<String> id,List<String> dept_id,List<String> user_id,long current,long size){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_id!=null,"dept_id",dept_id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return sysDeptUserRelMapper.selectPage(new Page<SysDeptUserRel>(current,size),queryWrapper).getRecords();
    }

    /**
     * ===================================================================================================
     * 基础删除
     * ===================================================================================================
     */

    /**
     * 删除组织信息
     */
    public boolean delSysDept(String id,String dept_name,String parent_dept_id,String dept_type,String creater_id,String del_flag){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(dept_name),"dept_name",dept_name);
        queryWrapper.in(!StringUtils.isEmpty(parent_dept_id),"parent_dept_id",parent_dept_id);
        queryWrapper.in(!StringUtils.isEmpty(dept_type),"dept_type",dept_type);
        queryWrapper.in(!StringUtils.isEmpty(creater_id),"creater_id",creater_id);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        sysDeptMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除组织信息
     * 列表删除
     */
    public boolean delSysDeptList(List<String> id,List<String> dept_name,List<String> parent_dept_id,List<String> dept_type,List<String> creater_id,List<String> del_flag){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_name!=null,"dept_name",dept_name);
        queryWrapper.in(parent_dept_id!=null,"parent_dept_id",parent_dept_id);
        queryWrapper.in(dept_type!=null,"dept_type",dept_type);
        queryWrapper.in(creater_id!=null,"creater_id",creater_id);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        sysDeptMapper.delete(queryWrapper);
        return true;
    }

    /**
     * 删除用户组织信息
     */
    public boolean delSysDeptUser(String id,String dept_id,String user_id){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(dept_id),"dept_id",dept_id);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        sysDeptUserRelMapper.delete(queryWrapper);
        return true;
    }

    /**
     * 删除用户组织信息
     * 列表删除
     */
    public boolean delSysDeptUserList(List<String> id,List<String> dept_id,List<String> user_id){
        QueryWrapper<SysDeptUserRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(dept_id!=null,"dept_id",dept_id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        sysDeptUserRelMapper.delete(queryWrapper);
        return true;
    }

    /**
     * 逻辑删除
     * true:不删除
     * flase：删除
     */
    public boolean logicDelDept(String id,boolean b){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        if(b){
            updateWrapper.set("del_flag","1");
        }else {
            updateWrapper.set("del_flag","0");
        }
        sysDeptMapper.update(null,updateWrapper);
        return true;
    }

    /**
     * ===================================================================================================
     * 基础更新
     * ===================================================================================================
     */

    /**
     * 修改组织信息
     */
    public boolean updateDept(String id,String name){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("dept_name",name);
        sysDeptMapper.update(null,updateWrapper);
        return true;
    }

    /**
     * ===================================================================================================
     * 基础新增
     * ===================================================================================================
     */

    /**
     * 增加组织
     */
    public boolean insertDept(SysDept sysDept){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysDept.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysDeptMapper.insert(sysDept);
        return true;
    }

    /**
     * 将用户添加进组织
     */
    public boolean insertUserDept(String userid,String deptid){
        SysDeptUserRel sysDeptUserRel = new SysDeptUserRel();
        sysDeptUserRel.setDeptId(deptid);
        sysDeptUserRel.setUserId(userid);
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysDeptUserRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysDeptUserRelMapper.insert(sysDeptUserRel);
        return true;
    }
    /**
     * 批量
     * 增加组织
     * 需要随便向pojo中输入一个id
     */
    public int insertDeptList(List<SysDept> sysDepts){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<sysDepts.size();i++){
            sysDepts.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return sysDeptMapper.insertBatchSomeColumn(sysDepts);
    }
    /**
     * 批量
     * 将用户添加进组织
     * 需要随便向pojo中输入一个id
     */
    public int insertUserDeptList(List<SysDeptUserRel> sysDeptUserRels){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<sysDeptUserRels.size();i++){
            sysDeptUserRels.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return sysDeptUserRelMapper.insertBatchSomeColumn(sysDeptUserRels);
    }
    /**
     * ===================================================================================================
     * 级联删除
     * ===================================================================================================
     */

    /**
     * 级联删除组织
     * @param dept_id
     * @return
     */
    public boolean delCascadeDept(String dept_id){
        delSysDeptUser(null,dept_id,null);
        delSysDept(null,null,dept_id,null,null,null);
        delSysDept(dept_id,null,null,null,null,null);
        return true;
    }
    /**
     * ===================================================================================================
     * 复杂查询
     * ===================================================================================================
     */
    /**
     * 查询改组织及其上级组织
     */
    public DeptAndParent selectDeptAndParent(String dept_id){
        DeptAndParent list = sysDeptMapper.selectJoinOne(DeptAndParent.class,
                new MPJQueryWrapper<SysDept>()
                        .selectAll(SysDept.class)
                        .select("t1.dept_name parent_name")
                        .leftJoin("sys_dept t1 on t.parent_dept_id = t1.id")
                        .eq("t.id",dept_id));
        return list;
    }
    /**
     * 查询改组织及其上级组织
     */
    public List<DeptAndParent> selectDeptAndParentList(List<String> dept_id){
        List<DeptAndParent> list = sysDeptMapper.selectJoinList(DeptAndParent.class,
                new MPJQueryWrapper<SysDept>()
                        .selectAll(SysDept.class)
                        .select("t1.dept_name parent_name")
                        .leftJoin("sys_dept t1 on t.parent_dept_id = t1.id")
                        .in("t.id",dept_id));
        return list;
    }
}
